package com.yunsuo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunsuo.bean.*;
import com.yunsuo.service.*;

public class CallHttp {

    public static void request(int l_id, String l_uuid) {
        final HttpConnectionFinal httpConnection = new HttpConnectionFinal();

        //本地门锁的uuid号，保存在数据库中
        LockRaspberry lock = new LockRaspberry(l_id, l_uuid, null,
                null, null, null, null, null,
                null, null);
        String jsonString = JSON.toJSONString(lock);
        //System.out.println("lock=    " + jsonString);
        final String info = "info=" + jsonString;
        //System.out.println("info=====" + info);
        String result = httpConnection.connection("/raspberry/receiveRequest", info);
        try {
            JSONObject jsonObject = JSON.parseObject(result);
            String u_id = jsonObject.getString("iUserId");
            if (jsonObject.getInteger("iIsUser") == 1) {
                //操作用户信息指令
                //System.out.println("iIsUser");
                String userinfo = jsonObject.getString("iUserInfo");
                //System.out.println(userinfo);
                JSONObject jsonObject1 = JSON.parseObject(userinfo);
                String method = jsonObject1.getString("method");
                String Lid = jsonObject1.getString("Lid");
                String Iuuid = jsonObject1.getString("Iuuid");
                if (method.equals("getImage")) {
                    //System.out.println("getImage");
                    ImageDemo.getImage(Lid, Iuuid);
                }
            } else if (jsonObject.getInteger("iIsFinger") == 1) {
                //操作指纹命令
                //System.out.println("iIsFinger");
                String fingerinfo = jsonObject.getString("iFingerInfo");
                //System.out.println(fingerinfo);
                //System.out.println(fingerinfo);
                JSONObject jsonObject1 = JSON.parseObject(fingerinfo);
                String method = jsonObject1.getString("method");
                if (method.equals("add")) {
                    //System.out.println("add Finger");
                    String fName = jsonObject1.getString("fName");
                    //修改SingletonLazy中的addFinger为1，关闭搜索指纹python，开启添加指纹python
                    //System.out.println("FingerMethod=" + SingletonLazy.getInstance().getMethod());
                    SingletonLazy singletonLazy = SingletonLazy.getInstance();
                    //把method置1，表示现在临界资源指纹模块要做的是添加指纹操作。
                    singletonLazy.changeMethod(1);
                    while (true) {
                        if (singletonLazy.P_IO() > -1) {
                            //System.out.println("addFinger get IO OK");
                            //System.out.println("IO=" + singletonLazy.getIOSource());
                            Thread.sleep(200);
                            AddFingerPrint.add(u_id, l_id, fName);
                            singletonLazy.V_IO();
                            singletonLazy.changeMethod(0);
                            Thread.sleep(200);
                            break;
                        }
                    }
                } else if (method.equals("del")) {
                    String uuid = jsonObject1.getString("fUuid");
                    DelFingerPrint.deleteFinger(uuid);
                } else if (method.equals("addFingerFile")) {
                    //System.out.println("addFingerFile");
                    String path = jsonObject1.getString("path");
                    String fName = jsonObject1.getString("fName");
                    String userID = jsonObject1.getString("userID");
                    //System.out.println("path=" + path);
                    //System.out.println("fName=" + fName);
                    //System.out.println("userID=" + userID);
                    SingletonLazy singletonLazy = SingletonLazy.getInstance();
                    //把method置2，表示现在临界资源指纹模块要做的是添加指纹操作。
                    singletonLazy.changeMethod(2);
                    while (true) {
                        if (singletonLazy.P_IO() > -1) {
                            //System.out.println("addFingerFile get IO OK");
                            //System.out.println("IO=" + singletonLazy.getIOSource());
                            Thread.sleep(200);
                            //System.out.println("++++++++++++++++++++");
                            AddFingerPrint.addFingerFile(path, fName, l_id, Integer.parseInt(userID));
                            //System.out.println("++++++++++++++++++++");
                            singletonLazy.V_IO();
                            singletonLazy.changeMethod(0);
                            Thread.sleep(200);
                            break;
                        }
                    }
                    //把指纹文件存入指纹模块
                    //把指纹信息保存到数据库
                }
            } else if (jsonObject.getInteger("iIsKey") == 1) {
                //操作密码指令
                //System.out.println("iIsKey");
                String keyInfo = jsonObject.getString("iKeyInfo");
                //System.out.println("keyinfo= " + keyInfo);
                JSONObject jsonObject1 = JSON.parseObject(keyInfo);
                String method = jsonObject1.getString("method");
                //System.out.println("method=" + method);
                if (method.equals("modify")) {
                    ModifyKey.getAll(u_id, l_id, keyInfo);
                } else if (method.equals("addTempraryKey")) {
                    //添加临时门锁信息
                    //System.out.println("111");
                    ModifyKey.insert(keyInfo);
                } else if (method.equals("delTempraryKey")) {
                    ModifyKey.del(keyInfo);
                } else if (method.equals("addMainKey")) {

                }
            } else if (jsonObject.getInteger("iIsLock") == 1) {
                //操作lock指令
                //System.out.println("iIsLock");
                String lockInfo = jsonObject.getString("iLockInfo");
                //System.out.println(lockInfo);
                JSONObject jsonObject1 = JSON.parseObject(lockInfo);
                String method = jsonObject1.getString("method");
                if (method.equals("openDoor")) {
                    //System.out.println("-------------------opendoor");
                    LockService.openDoor();
                }

            } else {
                //System.out.println("task OK");
            }
            //System.out.println("one task OK");

        } catch (Exception e) {
            //System.out.println(e.getMessage());
        }
    }
}
